<?php
/**
 * SSO 验证用户返回接口
 * @author shej E-mail:28708@qq.com
 */
require_once '../../dz_framework/init.php';

$token = isset($_REQUEST['token'])?$_REQUEST['token']:"";

//$token = '86f1b678db34c32923d029b3a9a1dea9c1311512000000015806409663226af2f3114485aa4e0a23b4';

if($token)
{
	$str_host = sha1($_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'] );	
	$token_user_id = substr($token,40,10);
	
	$format_user_id =  preg_replace('/^0+/','',$token_user_id);
	$user_info = DB::fetch_first('select user_name,user_nickname,user_realname,user_role_id,class_id,dept_id,gender,user_phone,user_email from '.DB::table('users')." where user_id='".$format_user_id."'");	
	
	$check_token = $str_host.$token_user_id.md5($user_info['user_name']);
	
	if( $token == $check_token)
	{
		if($user_info['user_role_id']==21)
		{
			$user_info['role_name']=='学生';
		}elseif($user_info['user_role_id']==22) {
			$user_info['role_name']=='老师';
		}
		$data = array('user_id' => $format_user_id,
					  'user_name' => $user_info['user_name'],
					  'user_nickname' => $user_info['user_nickname'],
					  'user_realname' => $user_info['user_realname'],
					  'role_id' => $user_info['user_role_id'],
					  'role_name' => $user_info['role_name'],
					  'class_id' => $user_info['class_id'],
					  'class_name' => $user_info['class_name'],
					  'grade_num' => $user_info['grade_num'],
					  'class_num' => $user_info['class_num'],
					  'school_id' => $user_info['school_id'],
					  'school_name' => $user_info['school_name'],
					  'gender' => $user_info['gender'],
					  'user_phone' => $user_info['user_phone'],
					  'user_email' => $user_info['user_email'],
					  'message' => 'success'
					  );
		if($user_info['class_id'])
		{
			$class_info = DB::fetch_first("select * from ".DB::table('class')." where class_id='".$user_info['class_id']."'");
			$data['grade_num'] = $class_info['grade_num'];
			$data['class_num'] = $class_info['class_num'];			
		}
		if($user_info['dept_id'])
		{
			$class_info = DB::fetch_first("select d_name from ".DB::table('dictionary')." where d_id='".$user_info['dept_id']."'");
			$data['school_name'] = $class_info['d_name'];				
		}
		
	}else{
		$data = array('token' => $token,					 
					  'message' => 'error'
					  );		
	}
	
}else{
		$data = array('token' => $token,					 
					  'message' => 'error'
					  );	
}

echo json_encode($data); exit;